<?php
/**
 * Created by PhpStorm.
 * User: chindor
 * Date: 2017/4/20
 * Time: 10:41
 */

namespace app\api\modules\v1\market\controllers;
use app\api\components\ApiController;
use app\api\modules\v1\market\models\AppUser;
use app\api\modules\v1\market\models\Auth_z;
use app\api\modules\v1\market\models\Message;
use app\api\modules\v1\market\models\Project_z;
use app\api\modules\v1\market\models\SessionMessage;
use app\api\modules\v1\market\models\User_z;
use app\models\base\Role;
use app\models\base\Svn;
use yii\helpers\Url;

class ProjectController extends ApiController
{
    public function init()
    {
        parent::init();
    }

    /**
     * 获得项目列表的信息
     */
    public function actionProject_list()
    {
        $get = \yii::$app->request->get();
        if(empty($get['user_id'])){
            return self::sendMessageJson(Message::MISS_ID);
        }
        $project_list = Project_z::get_list($get);
        if(empty($project_list)){
            return self::sendMessageJson(Message::EMPTY_DATA);
        }

        return self::sendMessageJson(Message::SUCCESS,self::$CODE_SUC,$project_list);

    }

    /**
     * @return mixed     项目的添加
     */
    public function actionAdd()
    {
        $post = \yii::$app->request->post();
        //验证token
        if(!(new Auth_z())->auth($post)){
            return self::sendMessageJson(Message::AUTH_FAIL);
        }

        if(!Project_z::add_project( $post )){
            return self::sendMessageJson(\yii::$app->session->getFlash('error'));
        }else{
            return self::sendMessageJson(Message::INSERT_SUCCESS,self::$CODE_SUC);
        }
    }

    public function actionEdit()
    {
        $post = \yii::$app->request->post();

        //验证token
        if(!(new Auth_z())->auth($post)){
            return self::sendMessageJson(Message::AUTH_FAIL);
        }

        if(empty($post['project_id'])){
            return self::sendMessageJson(Message::MISS_PROJECT_ID);
        }

        $validate = \yii\base\DynamicModel::validateData(ActivityController::handler_post(['user_id','member_id','role_id']),[
            [['user_id'],'required','message'  =>'用户id不能为空'],
            [['member_id'],'required','message'=>'成员不能为空'],
            [['role_id'],'required','message'=>'角色不能为空']
        ]);

        if($validate -> hasErrors()){
            return self::sendMessageJson(array_values($validate->getFirstErrors())[0]);
        }
        if(!Project_z::edit_project( $post )){
            return self::sendMessageJson(\yii::$app->session->getFlash('error'));
        }else{
            return self::sendMessageJson(Message::OPERATE_SUCCESS,self::$CODE_SUC);
        }
    }


    /**
     * @return mixed    获取所有的用户列表数据
     */
    public function actionUser_list()
    {
        $data = User_z::user_list();
        return self::sendMessageJson(Message::SUCCESS,self::$CODE_SUC,$data);
    }

    public function actionUser_list_advanced()
    {
        if(empty(\yii::$app->request->get('project_id'))){
            return self::sendMessageJson(Message::MISS_PROJECT_ID);
        }
        $role_list = Role::find()->all();
        return self::sendMessageJson(Message::SUCCESS,self::$CODE_SUC,['user_list'=>User_z::user_list_advanced(),'role_list'=>$role_list]);
    }

    /** 项目的关闭
     * @return array     */
    public function actionClose()
    {
        $post = \yii::$app->request->post();

        //验证token
        if(!(new Auth_z())->auth($post)){
            return self::sendMessageJson(Message::AUTH_FAIL);
        }

        if(empty($post['project_id'])){
            return self::sendMessageJson(Message::MISS_PROJECT_ID);
        }

        if(!Project_z::close($post['project_id'],$post['user_id'])){
            return self::sendMessageJson(SessionMessage::getMess());
        }

        return self::sendMessageJson(Message::OPERATE_SUCCESS,self::$CODE_SUC);
    }

    /**项目的删除
     * @return array
     */
    public function actionDelete()
    {
        $post = \yii::$app->request->post();

        //验证token
        if(!(new Auth_z())->auth($post)){
            return self::sendMessageJson(Message::AUTH_FAIL);
        }

        if(empty($post['project_id'])){
            return self::sendMessageJson(Message::MISS_PROJECT_ID);
        }

        if(!Project_z::clear($post['project_id'],$post['user_id'])){
            return self::sendMessageJson(SessionMessage::getMess());
        }

        return self::sendMessageJson(Message::OPERATE_SUCCESS,self::$CODE_SUC);
    }


    /**项目的开启
     * @return array
     */
    public function actionOpen()
    {
        $post = \yii::$app->request->post();

        //验证token
        if(!(new Auth_z())->auth($post)){
            return self::sendMessageJson(Message::AUTH_FAIL);
        }

        if(empty($post['project_id'])){
            return self::sendMessageJson(Message::MISS_PROJECT_ID);
        }

        if(!Project_z::open($post['project_id'],$post['user_id'])){
            return self::sendMessageJson(SessionMessage::getMess());
        }

        return self::sendMessageJson(Message::OPERATE_SUCCESS,self::$CODE_SUC);
    }

    /**svn信息的添加
     * @return array
     */
    public function actionAddSvn()
    {
        $post = \yii::$app->request->post();

        //验证token
        if(!(new Auth_z())->auth($post)){
            return self::sendMessageJson(Message::AUTH_FAIL);
        }

        if(!Svn::saveSvn($post)){
            return self::sendMessageJson(SessionMessage::getMess());
        }
        return self::sendMessageJson(Message::OPERATE_SUCCESS,self::$CODE_SUC);
    }

    /**svn的保存
     * @return array
     */
    public function actionSaveSvn()
    {
        return $this -> actionAddSvn();
    }

    /**svn配置信息的获取
     * @return array
     */
    public function actionGetSvn()
    {
        $get = \yii::$app->request->get();
        if(empty($get['project_id'])){
            return self::sendMessageJson(Message::MISS_PROJECT_ID);
        }

        if(empty($get['user_id'])){
            return self::sendMessageJson(Message::EMPTY_USER);
        }

        if(!$data = Svn::getSvn(\yii::$app->request->get())) {
            return self::sendMessageJson(SessionMessage::getMess());
        }
        return self::sendMessageJson(Message::OPERATE_SUCCESS,self::$CODE_SUC,$data);
    }







}